# import re
#
# from gxl_ai_utils.utils import utils_file
#
# input_text_path = ""
# # utils_file.do_get_list_for_wav_dir(input_text_path,suffix=".json", recursive=True)
# def process_text2(text, task_tag):
#     # 1. 删除汉字左右两侧的空格
#     text = re.sub(r'\s*([\u4e00-\u9fff])\s*', r'\1', text)
#     # 2. 将英文转成小写
#     if task_tag == "<TRANSCRIBE>":
#         text = text.lower()
#     # 3. 删除 < 和 > 符号两侧的空格
#     text = re.sub(r'\s*<\s*', '<', text)
#     text = re.sub(r'\s*>\s*', '>', text)
#     return text
#
# input_str = "方式撒公司 如歌如是方式的说法是 胜多负少 是 的 第三方hello world，fsfs 凝视对方 hello Hwerw sfsHDAD ."
# print(process_text2(input_str, "<s>"))
#
# def check_wav_format(s):
#     match = re.fullmatch(r"wav_(\d+)", s)
#     if match:
#         return True, int(match.group(1))
#     else:
#         return False, -1
#
# print(check_wav_format("wav_123"))   # 输出: (True, 123)
# print(check_wav_format("wav_abc"))   # 输出: (False, -1)
# print(check_wav_format("wave_123"))  # 输出: (False, -1)
#
# def insert_at_position(lst, item_str, position):
#     """
#     将 item_str 插入到 lst 的第 position 个位置（1-based），
#     若 lst 长度不足则以 "-1" 填充至目标长度后再插入。
#     """
#     index = position - 1
#     # 一次性计算需要补充的 "-1" 数目并批量 extend
#     if len(lst) < position:
#         lst.extend(["-1"] * (position - len(lst)))
#     lst[index] = item_str
#     print(lst)
#
# # 随机排列1-10 之间的数字
# num_list = [4, 2, 8, 1,2,3,4,4,5,6,7,8,8, 9, 3, 6, 5, 7, 10]
# res_list = []
# for num in num_list:
#     insert_at_position(res_list, f'wav_{num}', num)
# print(res_list)
#
# def check_txt_format(s):
#     match = re.fullmatch(r"txt_(\d+)", s)
#     if match:
#         return True, int(match.group(1))
#     else:
#         return False, -1
#
# print(check_txt_format("txt_123"))   # 输出: (True, 123)
# print(check_txt_format("txt_abc"))   # 输出: (False, -1)
# print(check_txt_format("txt"))  # 输出: (False, -1)
#
# input_list =  [ 800,   21, 2809, 1609,   24,  568,  568,  568,  568, 3910,
#       3910,   24,   24, 1796, 1513,   10,  768,  768,  768,   10,
#        882,   26,   36,  760, 3898, 3898, 3898,   53, 3898, 3898,
#       3898, 3898, 3898,   53, 2684, 2684, 1949, 1949,   36,   36,
#       1734,   45,  822, 3265, 3910,  568, 2786, 2786, 2037,  571,
#       2385, 1796, 2148,  532, 3600, 2246,  927,   41, 1718, 1615,
#         54,  432, 3636, 2285, 1083, 1937, 1013,   84,  386,   41,
#       1615, 3741, 2243, 1122, 1915,  349,  349,  882, 3844, 1333,
#       3554,  329, 1289, 4082,  131, 2032, 3121, 3121, 3121, 3064,
#       1018, 1018, 1018, 1018, 2230, 2564, 1385, 1117, 1117, 1117,
#       1117, 1117, 1117, 1117,   36,   36, 1117, 1117,   36,   36,
#         36, 1117, 1117, 1117]
#
# print(input_list)
#
import re

import numpy as np
# def vad_energy_based(signal, threshold=0.8):
#     """
#     基于能量的语音活动检测（VAD）
#
#     参数:
#     - signal: np.ndarray, shape=(512,), 输入的音频帧，长度为512采样点，采样率为16kHz
#     - threshold: float, 概率阈值，大于该值认为是有语音（默认0.8）
#
#     返回:
#     - is_speech: bool, 是否为语音
#     - prob: float, 语音的概率，范围[0, 1]
#     """
#     if not isinstance(signal, np.ndarray):
#         signal = np.array(signal)
#
#     # 归一化输入音频
#     signal = signal.astype(np.float32)
#     signal -= np.mean(signal)
#     signal /= (np.std(signal) + 1e-8)
#
#     # 计算短时能量（归一化能量值作为概率）
#     energy = np.sum(signal ** 2) / len(signal)
#     print(energy)
#
#     # 假定最大可能能量为1（标准化后），将能量映射到 [0, 1]
#     prob = np.clip(energy, 0.0, 1.0)
#
#     # # 返回是否为语音（大于阈值）以及概率
#     # is_speech = prob > threshold
#     return prob

# 测试
def vad_decibel_based(signal, db_threshold=-20):
    """
    使用分贝值检测是否有人说话。

    参数:
    - signal: np.ndarray, shape=(512,), 输入音频帧
    - db_threshold: float, 分贝阈值（默认 -40 dB，约等于安静房间）

    返回:
    - is_speech: bool, 是否为语音
    - db: float, 信号的分贝值
    """
    if not isinstance(signal, np.ndarray):
        signal = np.array(signal)
    signal = signal.astype(np.float32)

    # 去直流分量
    signal = signal - np.mean(signal)

    # RMS 计算
    rms = np.sqrt(np.mean(signal ** 2))

    # 避免 log(0)
    if rms < 1e-10:
        db = -100.0  # 非常安静，近似静音
    else:
        db = 20 * np.log10(rms / 1.0)

    # 判断是否为语音
    is_speech = db > db_threshold
    return db

# signal = np.random.randn(512)
# print(signal)
# prob = vad_decibel_based(signal)
# print(prob)


# 过滤自我介绍
self_list = [
    # 不指定名称
    "",

    # 原有 LLM 名称及变体
    "MOSS", "Moss", "moss", "MOSS Assistant", "MOSS助手",
    "QWEN", "Qwen", "qwen", "QWEN Assistant", "QWEN助手",
    "小智", "小智机器人", "小智助手", "小智AI助手",

    # 通用英文助手/AI 名称
    "VirtualAssistant", "virtual assistant",
    "Helper", "helper", "ChatBot","chat bot", "chatbot", "Chat Bot",
    "AI Agent", "AI agent",  "ChatGPT", "chatgpt",

    # 常见英文名字
    " Alice ", " alice ", " Bob ", " bob ", " Charlie ", " charlie ",
    " Dave ", " dave ", " Eve ", " eve ", " Grace ", " grace ", " Tom ",

    # 常见中文名字
    "小明", "小红", "小刚", "晓明", "晓红", "阿强", "阿丽",
    "丽丽", "婷婷", "王磊", "李娜", "张伟", "赵敏", "刘洋", "陈晨",
    "小李", "小王", "小赵", "小周", "小吴", "小马",

    # 中文名字 + 助手/AI 后缀
    "小明助手", "小红小助手", "小刚AI", "晓明助手", "阿强Bot",
    "阿丽小助手", "王磊AI助手", "李娜AI", "张伟助手", "赵敏Bot",
    "刘洋AI小助手", "陈晨智能助手",

    # 混合中英文风格
    "MOSS小助手", "QWEN小助手", "小智Bot", "Assistant小智", "AI小智",
    "ChatBot小明", "VirtualAssistant李娜",

]
escaped = [re.escape(w) for w in self_list if w]
pattern = re.compile(r"(" + "|".join(escaped) + r")")

text = "MOSS小助手，你好！"
matches = pattern.findall(text)
if matches:
    print("出现了：", set(matches))
else:
    print("都没出现。")

text = "嘻嘻嘻,Qwen你好！"
matches = pattern.findall(text)
if matches:
    print("出现了：", set(matches))
else:
    print("都没出现。")

text = "嘻嘻qwen嘻，你好！"
matches = pattern.findall(text)
if matches:
    print("出现了：", set(matches))
else:
    print("都没出现。")

text = "chat bot嘻嘻嘻，你qwen好！"
matches = pattern.findall(text)
if matches:
    print("出现了：", set(matches))
else:
    print("都没出现。")